<?php

class WebView extends BaseView {
	
	const memberName = 'memberName' ;
	const memberPassword = 'memberPassword' ;
	const memberSsn = 'memberSsn' ;
	const memberId = 'memberId' ;
	const boatType = 'boatType' ;
    const boatLength = 'boatLength' ;
    const boatId = 'boatId' ;
    const removeBoat = 'removeBoat' ;
	
	public $m_isLoggedIn ;
	
	public function getView() {
		if(isset($_GET['page'])) {
			return $_GET['page'] ;
		}
		
		return false ;
		
	}
	
	public function getUserInfo() {
		$userInfo = array() ;
		
		if(isset($_POST[$this::memberName]) && isset($_POST[$this::memberSsn])) {
			$userInfo['name'] = $_POST[$this::memberName] ;
            $userInfo['ssn'] = $_POST[$this::memberSsn] ;
			$userInfo['password'] =  $_POST[$this::memberPassword] ;

            if (isset($_POST[$this::memberId])) {
                $userInfo['memberId'] = $_POST[$this::memberId] ;
            }
			return $userInfo ;
		}
			
		return false ;
		
		
	}
	
	public function getBoatInfo() {
		$boatInfo = array() ;
		
		if(isset($_POST[$this::boatLength]) && isset($_POST[$this::boatType]) && isset($_POST[$this::memberId])) {
			$boatInfo['type'] = $_POST[$this::boatType] ;
			$boatInfo['length'] = $_POST[$this::boatLength] ;
                        $boatInfo['memberId'] = $_POST[$this::memberId] ;

                        if (isset($_POST[$this::boatId])) {
                            $boatInfo['boatId'] = $_POST[$this::boatId] ;
                        }

			return $boatInfo ;
		}
			
		return false ;
		
		
	}
	public function getMemberId() {
		if(isset($_GET[$this::memberId])) {
			return $_GET[$this::memberId] ;
		}
		
		return false ;
        }

    public function getBoatId() {
        if(isset($_GET[$this::boatId])) {
            return $_GET[$this::boatId] ;
        }

        return false ;
    }
	
	public function setLoginStatus($a_isLoggedIn) {
		$this->m_isLoggedIn = $a_isLoggedIn ;
	}
	
	public function menu() {
		
		$returnString = "<ul class='menu'>" ;
		
		if($this->m_isLoggedIn) {
			$returnString .= "<li><a href='?page=registerMember'>Registrera medlem</a></li>" ;
		}
		$returnString .= "<li><a href='?page=listMembersCompact'>Lista medlemmar (kompakt)</a></li>" ;
		$returnString .= "<li><a href='?page=listMembersFull'>Lista medlemmar (fullständig)</a></li>" ;
		$returnString .= "<li><a href='?page=search'>Sök</a></li>" ;
		$returnString .= '</ul>' ;
		
		$returnString .= "<div id='clr'></div>" ;
		
		return $returnString ;
		
	}
	
    public function renderMemberForm($a_page, MemberClass $a_object = null) {

        $name = isset($a_object->name) ? $a_object->name : null ; 
        $ssn = isset($a_object->ssn) ? $a_object->ssn : null ;
		$password = isset($a_object->password) ? $a_object->password : null ;
		
		
		if($this->m_isLoggedIn) {
		
			$returnString = "
			
				<form action='?page=".$a_page."' method=post> 
					<label for='".$this::memberName."'>Namn: </label>
					<input type='text' name='".$this::memberName."' id='".$this::memberName."' value='".$name."' /><br />
					<label for='".$this::memberPassword."'>Lösenord: </label>
					<input type='text' name='".$this::memberPassword."' id='".$this::memberPassword."' value='".$password."' /><br />
					<label for='".$this::memberSsn."'>Personnummer: </label>
	                                <input type='text' name='".$this::memberSsn."' id='".$this::memberSsn."' value='".$ssn."' /><br />
	                    " ;
	
	            if (isset($a_object->memberId)) {
	                $returnString .= "<input type='hidden' name='".$this::memberId."' id='".$this::memberId."' value='".$a_object->memberId."' />" ;
	            }
		    $returnString .= "<input type='submit' />
				</form>
		    " ;
		
		} else {
			$returnString = "<p>Du är inte inloggad.</p>" ;
		}
		
		return $returnString ;
		
	}
	
	public function renderBoatForm($a_page, BoatClass $a_object = null) {
        $returnString = "" ;	
            
        $type = isset($a_object->type) ? $a_object->type : null ; 
        $length = isset($a_object->length) ? $a_object->length : null ;

        $memberId = isset($a_object->memberId) ? $a_object->memberId : $this->getMemberId() ;

        $boatTypes = array('Segelbåt', 'Motorseglare', 'Motorbåt', 'Kajak/Kanot', 'Övrigt') ;

		if($this->m_isLoggedIn) {
			$returnString .= "
				<form action='?page=$a_page' method=post> 
					<label for='".$this::boatType."'>Typ: </label>
					<select name='".$this::boatType."' id='".$this::boatType."'>" ;
	
	                foreach($boatTypes as $boatType) {
	                    $selected =($boatType == $type) ? "selected=selected" : null ;
	                    $returnString .= "<option value='".$boatType."' $selected>$boatType</option>" ;
	                }
	
	        $returnString .= "
	            </select><br />
				<label for='".$this::boatLength."'>Längd (cm): </label>
				<input type='text' name='".$this::boatLength."' id='".$this::boatLength."' value='".$length."' /><br />
	            <input type='hidden' name='".$this::memberId."' value='".$memberId."' />
	        ";
	
	        if (isset($a_object->boatId)) {
	                $returnString .= "<input type='hidden' name='".$this::boatId."' id='".$this::boatId."' value='".$a_object->boatId."' />" ;
	        }
	
			$returnString .= "		<input type='submit' />
				</form>
			
			" ;
		} else {
			$returnString .= "<p>Du är inte inloggad.</p>" ;
		}
		
		return $returnString ;
		
    }
	
	public function renderMembersCompact($a_members) {
			
		$returnString = "" ;	
		
		$returnString .= "<table>
			<tr>
				<th>Id</th><th>Namn</th><th>Antal båtar</th><th></th>
			</tr>" ;
		
		foreach($a_members as $member) {

			$returnString .= "
			<tr>
				<td>". $member->memberId ."</td>
                                <td><a href='?page=showMember&memberId=". $member->memberId ."'>". $member->name ."</a></td>
				<td>". count($member->getBoats()) ."</td>" ;
			if($this->m_isLoggedIn) {
	            $returnString .= " 
		            <td>
			            <a href='?page=registerBoat&memberId=". $member->memberId ."'>Registrera båt</a> | 
			            <a href='?page=removeMember&memberId=". $member->memberId ."'>Ta bort medlem</a> | 
			            <a href='?page=changeMember&memberId=". $member->memberId ."'>Editera medlem</a>
		            </td> " ;
			}
			$returnString .= "</tr>" ;
		} 
		$returnString .= "</table>" ;
		
		return $returnString ;
	}
	
	public function renderMembersFull($a_members) {
		$returnString = "" ;
		
		$returnString .= "
		<table>
			<tr>
				<th>Id</th><th>Namn</th><th>Personnummer</th><th>Båtar</th><th></th>
			</tr> " ;
			
		foreach($a_members as $member) {

			$returnString .= "
			<tr>
				<td>". $member->memberId ."</td>
				<td><a href='?page=showMember&memberId=". $member->memberId ."'>".$member->name."</a></td>
				<td>". $member->ssn ."</td>
				<td>" ;

			foreach($member->getBoats() as $boat) {
				$returnString .= $boat->type." (".$boat->length.") <br />" ;	
			}

					
			$returnString .= "
				</td> " ;
			if($this->m_isLoggedIn) {
	            $returnString .= " 
		            <td>
			            <a href='?page=registerBoat&memberId=". $member->memberId ."'>Registrera båt</a> | 
			            <a href='?page=removeMember&memberId=". $member->memberId ."'>Ta bort medlem</a> | 
			            <a href='?page=changeMember&memberId=". $member->memberId ."'>Editera medlem</a>
		            </td> " ;
			}
			$returnString .= "</tr> " ;

		} 

		$returnString .= "</table>" ; 
		
		return $returnString ;
        
        }

    public function showMember(MemberClass $a_object) {
        		
        	$returnString = "" ;

            $returnString .= "<p>MedlemsId: $a_object->memberId</p>" ;
            $returnString .=  "<p>Namn: $a_object->name</p>" ;
            $returnString .=  "<p>Personnummer: $a_object->ssn</p>" ;

            $returnString .=  "<p>Båtar : <br />" ;
            foreach($a_object->getBoats() as $boat) {

                $returnString .=  $boat->type." (".$boat->length.") <a href='?page=changeBoat&boatId=".$boat->boatId."'>Editera båt</a> | <a href='?page=removeBoat&boatId=".$boat->boatId."'>Ta bort</a> <br />" ;

            }
            
            return $returnString ;


        }
        
    public function showMessages($a_messages) {
            	
            $returnString = "" ;		
            	
            foreach($a_messages as $message) {
                $returnString .= "<p>".$message."</p>" ;
            }
			
			return $returnString ;
        }
	
}
